package com.regulareport.common.utils;

import com.regulareport.common.enums.FrequencyType;
import com.regulareport.common.utils.poi.PoiUtil;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegularUtil {
    private static final Logger log = LoggerFactory.getLogger(RegularUtil.class);
    private static final Pattern pattern = Pattern.compile("(\\d{4})年(\\d{2})月(\\d{2})日");


    public static boolean checkDataDate(Sheet sheet,  String dataDate) {

        for (int i = 1; i < 4; i++) {
//            sheet.getcol
            Row row = sheet.getRow(i);
            for (int j = 0; j < row.getLastCellNum(); j++) {
                Cell cell = row.getCell(j);
                String cellValueAsString = PoiUtil.getCellValueAsString(cell);
                log.info("单元格值:{}", cellValueAsString);
                Matcher matcher = pattern.matcher(cellValueAsString);
                if (matcher.find()) {
                    String year = matcher.group(1);
                    String month = matcher.group(2);
                    String day = matcher.group(3);
                    log.info("year:{}, month:{}, day:{}", year, month, day);
                    return dataDate.equalsIgnoreCase(year + month + day);
                }
            }
        }

        return false;
    }


    public static String replaceKong(String a) {
        String s = a.replaceAll("\\s", "");
        s = s.replaceAll("\\u00A0", "");
        return s;
    }

    public static Date calReportTime(int frequency, int deadLine) {
        LocalDate localDate = LocalDate.now();
        LocalDate result;
        if (frequency == FrequencyType.QUARTER1.getCode() || frequency == FrequencyType.QUARTER2.getCode()) {
            LocalDate localDate1 = LocalDate.of(localDate.getYear(), localDate.getMonthValue(), deadLine);
            if (localDate.isAfter(localDate1)) {
                result = localDate1.plusMonths(1);
            } else {
                result = localDate1;
            }
        } else if (frequency == FrequencyType.HALF_YEAR.getCode()) {
            result = localDate;
        } else {
            switch (localDate.getMonthValue()) {
                case 1:
                case 2:
                case 3:
                    result = LocalDate.of(localDate.getYear(), 4, deadLine);
                    break;
                case 4:
                case 5:
                case 6:
                    result = LocalDate.of(localDate.getYear(), 7, deadLine);
                    break;
                case 7:
                case 8:
                case 9:
                    result = LocalDate.of(localDate.getYear(), 10, deadLine);
                    break;
                case 10:
                case 11:
                case 12:
                    result = LocalDate.of(localDate.getYear() + 1, 1, deadLine);
                    break;
                default:
                    result = localDate;
            }
        }

        return Date.from(result.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());
    }
}
